﻿using StoreProject.DAL;
using StoreProject.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StoreProject.BLL
{
    public class OrderBLL
    {
        private readonly OrderDAL _dal = new OrderDAL();

        public bool Insert(OrderEntity model)
        {
            bool res = false;
            int? result = _dal.Insert(model);
            if (result != null)
                res = (int)result > 0;
            return res;
        }

        public bool InsertOrUpdate(OrderEntity model)
        {
            bool res = false;
            int? result = 0;
            if (_dal.IsExists(model.Id))
            {
                _dal.Update(model);
            }
            else
            {
                _dal.Insert(model);
            }
            if (result != null)
                res = (int)result > 0;
            return res;
        }

        public bool Update(OrderEntity model)
        {
            bool res = false;
            int result = _dal.Update(model);
            res = (int)result > 0;
            return res;
        }

        public bool Delete(string id)
        {
            bool res = false;
            int result = _dal.Delete(id);
            res = (int)result > 0;
            return res;
        }

        public bool Delete(OrderEntity model)
        {
            bool res = false;
            int result = _dal.Delete(model);
            res = (int)result > 0;
            return res;
        }

        public OrderEntity Get(string id)
        {
            return _dal.Get(id);
        }

        public IEnumerable<OrderEntity> GetList()
        {
            return _dal.GetList();
        }

        public IEnumerable<OrderEntity> GetListEx(string finishedProductName)
        {
            return _dal.GetListEx(finishedProductName);
        }

        public IEnumerable<OrderEntity> GetListByWhere(string orderId)
        {
            return _dal.GetListByWhere(string.Format("OrderId='{0}'", orderId));
        }
        public int DeleteAll()
        {
            return _dal.DeleteAll();
        }
        public IEnumerable<OrderEntity> GetListByWhereEx(string orderId,string Batch, string Operator, string Customer, string FinishedProductName, string DomesticAndExportSales, string LineType, string SemiFinishedProductName, string ProcessFlowId,DateTime dateTimeCreateTimeBegin, DateTime dateTimeCreateTimeEnd)
        {
            string sql = " 1 = 1 ";
            if (!string.IsNullOrEmpty(orderId))
            {
                sql += " and OrderId like '%" + orderId + "%'";
            }
            if (!string.IsNullOrEmpty(Batch))
            {
                sql += " and Batch like '%" + Batch + "%'";
            }
            if (!string.IsNullOrEmpty(Operator))
            {
                sql += " and Operator like '%" + Operator + "%'";
            }
            if (!string.IsNullOrEmpty(Customer))
            {
                sql += " and Customer like '%" + Customer + "%'";
            }
            if (!string.IsNullOrEmpty(FinishedProductName))
            {
                sql += " and FinishedProductName like '%" + FinishedProductName + "%'";
            }
            if (!string.IsNullOrEmpty(DomesticAndExportSales))
            {
                sql += " and DomesticAndExportSales like '%" + DomesticAndExportSales + "%'";
            }
            if (!string.IsNullOrEmpty(LineType))
            {
                sql += " and LineType like '%" + LineType + "%'";
            }
            if (!string.IsNullOrEmpty(SemiFinishedProductName))
            {
                sql += " and SemiFinishedProductName like '%" + SemiFinishedProductName + "%'";
            }
            if (!string.IsNullOrEmpty(ProcessFlowId))
            {
                sql += " and ProcessFlowId like '%" + ProcessFlowId + "%'";
            }
            sql += " and date(CreateTime)>=date('" + dateTimeCreateTimeBegin + "')";
            sql += " and date(CreateTime)<=date('" + dateTimeCreateTimeEnd + "')";
            sql += " order by UpdateTime desc";

            return _dal.GetListByWhere(sql);
        }
    }
}
